#include "project.h"

DEVICE::DEVICE(string set_dev_path):dev_file_path(set_dev_path)
{
    if(this->Dev_Init() == false)
    {
        cout<<"设备打开异常！"<<endl;
        exit(-1);
    }
}


bool DEVICE::Dev_Init()
{
    if((this->dev_fd = open(this->dev_file_path.c_str(),O_RDWR)) == -1)
    {
        cerr<<"open"<<endl;
        return false;
    }
    return true;
}


bool DEVICE::Dev_Free()
{
    if(close(this->dev_fd) == -1)
    {
        cerr<<"close"<<endl;
        return false;
    }

    return true;
}


LCD::LCD(string lcd_dev_path):DEVICE(lcd_dev_path)
{
    this->lcd_w = 0;
    this->lcd_h = 0;
    this->mmap_star = nullptr;

    if(this->Lcd_Star() == false)
    {
        cout<<"LCD设备运行异常！"<<endl;
        exit(-1);
    }
}


bool LCD::Lcd_Star()
{
    //ioctl 获取LCD 的分辨率 1024 X 600

    //mmap();

    return true;
}


bool LCD::Dispaly_Pic(const char * pic_path) //默认居中显示
{
    //打开图片文件
    int bmp = open(pic_path,O_RDONLY);
    if(bmp == -1)
    {
        cerr<<"open ...";
        return false;
    }

    int bmp_w,bmp_h;
    int skip;
    //获取图片长和宽，计算图片像素点宽度是否能被4正常，计算每行像素点的补数
    lseek(bmp,18,SEEK_SET);
    if(read(bmp,&bmp_w,sizeof(int)) == -1)
    {
        cerr<<"read ...";
        return false;
    }

    if(read(bmp,&bmp_h,sizeof(int)) == -1)
    {
        cerr<<"read ...";
        return false;
    }

    if(bmp_w * 3 % 4 == 0)
    {
        skip = 0;
    }
    else
    {
        skip = 4 - bmp_w * 3 % 4;
    }


    //偏移54个字节
    lseek(bmp,54,SEEK_SET);

    //定义数组，读取像素点
    char buf[bmp_w * bmp_h * 3 + skip * bmp_h];
    if(read(bmp,buf,bmp_w * bmp_h * 3 + skip * bmp_h) == -1)
    {
        cerr<<"read ...";
        return false;
    }

    int * new_mmap = this->mmap_star + lcd_w * (lcd_w/2 - bmp_w/2) + (lcd_h/2 - bmp_h/2);
    //for循环 把像素点赋值给  映射指针
    for(int y=0,n=0; y<bmp_h; y++)
    {
        for(int x=0; x<bmp_w; x++,n+=3)
        {
            *(new_mmap + lcd_w * (lcd_h-1-y) + x) = 
            buf[n] << 0 | buf[n+1] << 8 | buf[n+2] << 16;
        }
        n+=skip;
    }


    //关闭图片文件
    if(close(bmp) == -1)
    {
        cerr<<"close ...";
        return false;
    }

    return true;
}


TOUCH::TOUCH(string touch_dev_path):DEVICE(touch_dev_path)
{
    this->touch_max_x = 1024;
    this->touch_max_h = 600;
    this->touch_min_x = 0;
    this->touch_min_h = 0;
    this->present_touch_x = -1; //当前触摸的x坐标
    this->present_touch_y = -1; //当前触摸的y坐标
}


bool TOUCH::Get_Touch_Inf()
{
    struct input_event touch;
    while(1)
    {
        if(read(this->DEVICE::dev_fd,&touch,sizeof(touch)) == -1)
        {
            cerr<<"read";
            return false;
        }

        if(touch.type == EV_ABS && touch.code == ABS_X) this->present_touch_x = touch.value; 
        if(touch.type == EV_ABS && touch.code == ABS_Y) this->present_touch_y = touch.value; 
        if(touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value == 0)
        {
            break;
        }
    }

    cout<<"X坐标:"<<this->present_touch_x<<"Y坐标:"<<this->present_touch_y<<endl;
    return true;
}